<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/10  10:18
 */
namespace app\admin\controller;
use think\Db;
use think\Controller;
class Regionalagent extends Common
{
    private $table_admin;
    /*
	*	构造函数
	*/
    function __construct(){
        parent::__construct();
        $this->table_admin = db('region');

    }

//区域代理
    public function region_list(){

        $phone = input('phone');

        if(!empty($phone)){
            $where['phone'] = $phone;
        }else{
            $where='';
        }

        $list = db('region')->where($where)->paginate(15)->each(function($item,$key){

           /* $user = db('user_account')->where('id',$item['uid'])->find();*/

            $item['region_money'] = $this->region_total($item['uid']);

            return $item;
        });

        $this->assign('list',$list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    public function del(){

        $id['id'] = input('id');

        $res = db('region')->delete($id);

        if(empty($res))return json(['msg'=>'更新失败']);
        return json(['msg'=>'更新成功']); 
    }

//会员详情    
    public function region_info(){
        $uid = input('uid');
        $users = db('member')->where('id',$uid)->select();
        $data = $this->next_member($uid);
        $this->assign('users',$users);
        $this->assign('team',$data);
        return $this->fetch();
    }


//区域代理修改    
    public function edit_region(){

        $id = input('id');
        if(!empty($id)){

            $region = db('region')->where('id',$id)->find();

            $this->assign('da',$region);
        }
       
        return $this->fetch();
    }

 
    public function doedit_region(){
        $id = input('id');
        $province = input('province');
        $city = input('city');
        $county = input('county');
        $name = input('name');
        $phone = input('phone');
        $status = input('status');
        $time = time();
        $data['province'] = $province;
        $data['city'] = $city;
        $data['county'] = $county;
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['status'] = $status;

            if(empty($id)){
                //添加
                $data['created_at'] = time();
                $res = db('region')->insert($data);
            }else{
                //修改

                $data['update_at'] = time();
                $res = db('region')->where(['id'=>$id])->update($data);
                if($status==3){
                    db('region')->delete($id);
                }
                
            }
            
            if(empty($res))return json(['msg'=>'更新失败']);
            return json(['msg'=>'更新成功']); 
    }         


//区域代理列表
    public function regional_lists(){

        return $this->fetch();
    }


            /**
     * 获取区域代理佣金
     */
    public function region_total($uid) {


        //判断是否区域代理
        $user = db('region')->where('uid',$uid)->find();
  

        $total = db('region_commission')->where('uid',$uid)->sum('commission');

        if(empty($total)){

            $total = 0;
        }

        return $total;

    }

}

